<template>
  <div class="panel-tab__content">
    <el-table :data="elementListenersList" border>
      <el-table-column label="序号" width="50px" type="index" />
      <el-table-column label="事件类型" min-width="80px" show-overflow-tooltip :formatter="row => listenerEventTypeObject[row.event]" />
<!--      <el-table-column label="事件id" min-width="80px" prop="id" show-overflow-tooltip />-->
      <el-table-column label="监听器类型" min-width="80px" show-overflow-tooltip :formatter="row => listenerTypeObject[row.listenerType]" />
      <el-table-column label="操作" width="90px">
        <template #default="{ row, $index }">
          <el-button size="small" type="text" @click="openListenerForm(row, $index)">编辑</el-button>
          <el-divider direction="vertical" />
          <el-button size="small" type="text" style="color: #ff4d4f" @click="removeListener(row, $index)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="element-drawer__button">
      <el-button size="small" type="primary" icon="el-icon-plus" @click="openListenerForm(null)">添加监听器</el-button>
    </div>

    <!-- 监听器 编辑/创建 部分 -->
    <el-drawer v-model="listenerFormModelVisible" title="任务监听器" :size="`${width}px`" append-to-body destroy-on-close>
      <el-form size="small" :model="listenerForm" label-width="96px" ref="listenerFormRef" @submit.native.prevent>
        <el-form-item label="事件类型" prop="event" :rules="{ required: true, trigger: ['blur', 'change'] }">
          <el-select v-model="listenerForm.event">
            <el-option v-for="i in Object.keys(listenerEventTypeObject)" :key="i" :label="listenerEventTypeObject[i]" :value="i" />
          </el-select>
        </el-form-item>
<!--        <el-form-item label="监听器ID" prop="id" :rules="{ required: true, trigger: ['blur', 'change'] }">-->
<!--          <el-input v-model="listenerForm.id" clearable />-->
<!--        </el-form-item>-->
        <el-form-item label="监听器类型" prop="listenerType" :rules="{ required: true, trigger: ['blur', 'change'] }">
          <el-select v-model="listenerForm.listenerType">
            <el-option v-for="i in Object.keys(listenerTypeObject)" :key="i" :label="listenerTypeObject[i]" :value="i" />
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="listenerForm.listenerType === 'classListener'"
          label="Java类"
          prop="class"
          key="listener-class"
          :rules="{ required: true, trigger: ['blur', 'change'] }"
        >
          <el-input v-model="listenerForm.class" clearable />
        </el-form-item>
        <el-form-item
          v-if="listenerForm.listenerType === 'expressionListener'"
          label="表达式"
          prop="expression"
          key="listener-expression"
          :rules="{ required: true, trigger: ['blur', 'change'] }"
        >
          <el-input v-model="listenerForm.expression" clearable />
        </el-form-item>
        <el-form-item
          v-if="listenerForm.listenerType === 'delegateExpressionListener'"
          label="代理表达式"
          prop="delegateExpression"
          key="listener-delegate"
          :rules="{ required: true, trigger: ['blur', 'change'] }"
        >
          <el-input v-model="listenerForm.delegateExpression" clearable />
        </el-form-item>
        <template v-if="listenerForm.listenerType === 'scriptListener'">
          <el-form-item
            label="脚本格式"
            prop="scriptFormat"
            key="listener-script-format"
            :rules="{ required: true, trigger: ['blur', 'change'], message: '请填写脚本格式' }"
          >
            <el-input v-model="listenerForm.scriptFormat" clearable />
          </el-form-item>
          <el-form-item
            label="脚本类型"
            prop="scriptType"
            key="listener-script-type"
            :rules="{ required: true, trigger: ['blur', 'change'], message: '请选择脚本类型' }"
          >
            <el-select v-model="listenerForm.scriptType">
              <el-option label="内联脚本" value="inlineScript" />
              <el-option label="外部脚本" value="externalScript" />
            </el-select>
          </el-form-item>
          <el-form-item
            v-if="listenerForm.scriptType === 'inlineScript'"
            label="脚本内容"
            prop="value"
            key="listener-script"
            :rules="{ required: true, trigger: ['blur', 'change'], message: '请填写脚本内容' }"
          >
            <el-input v-model="listenerForm.value" clearable />
          </el-form-item>
          <el-form-item
            v-if="listenerForm.scriptType === 'externalScript'"
            label="资源地址"
            prop="resource"
            key="listener-resource"
            :rules="{ required: true, trigger: ['blur', 'change'], message: '请填写资源地址' }"
          >
            <el-input v-model="listenerForm.resource" clearable />
          </el-form-item>
        </template>

        <template v-if="listenerForm.event === 'timeout'">
          <el-form-item label="定时器类型" prop="eventDefinitionType" key="eventDefinitionType">
            <el-select v-model="listenerForm.eventDefinitionType">
              <el-option label="日期" value="date" />
              <el-option label="持续时长" value="duration" />
              <el-option label="循环" value="cycle" />
              <el-option label="无" value="null" />
            </el-select>
          </el-form-item>
          <el-form-item
            v-if="!!listenerForm.eventDefinitionType && listenerForm.eventDefinitionType !== 'null'"
            label="定时器"
            prop="eventTimeDefinitions"
            key="eventTimeDefinitions"
            :rules="{ required: true, trigger: ['blur', 'change'], message: '请填写定时器配置' }"
          >
            <el-input v-model="listenerForm.eventTimeDefinitions" clearable />
          </el-form-item>
        </template>
      </el-form>

      <el-divider />
      <p class="listener-filed__title">
        <span><i class="el-icon-menu"></i>注入字段：</span>
        <el-button size="small" type="primary" @click="openListenerFieldForm(null)">添加字段</el-button>
      </p>
      <el-table :data="fieldsListOfListener" size="small" max-height="240" border fit style="flex: none">
        <el-table-column label="序号" width="50px" type="index" />
        <el-table-column label="字段名称" min-width="100px" prop="name" />
        <el-table-column label="字段类型" min-width="80px" show-overflow-tooltip :formatter="row => fieldTypeObject[row.fieldType]" />
        <el-table-column label="字段值/表达式" min-width="100px" show-overflow-tooltip :formatter="row => row.string || row.expression" />
        <el-table-column label="操作" width="100px">
          <template #default="{ row, $index }">
            <el-button size="small" type="text" @click="openListenerFieldForm(row, $index)">编辑</el-button>
            <el-divider direction="vertical" />
            <el-button size="small" type="text" style="color: #ff4d4f" @click="removeListenerField(row, $index)">移除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="element-drawer__button">
        <el-button size="small" @click="listenerFormModelVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="saveListenerConfig">保 存</el-button>
      </div>
    </el-drawer>

    <!-- 注入西段 编辑/创建 部分 -->
    <el-dialog title="字段配置" v-model="listenerFieldFormModelVisible" width="600px" append-to-body destroy-on-close>
      <el-form :model="listenerFieldForm" size="small" label-width="96px" ref="listenerFieldFormRef" style="height: 136px" @submit.native.prevent>
        <el-form-item label="字段名称：" prop="name" :rules="{ required: true, trigger: ['blur', 'change'] }">
          <el-input v-model="listenerFieldForm.name" clearable />
        </el-form-item>
        <el-form-item label="字段类型：" prop="fieldType" :rules="{ required: true, trigger: ['blur', 'change'] }">
          <el-select v-model="listenerFieldForm.fieldType">
            <el-option v-for="i in Object.keys(fieldTypeObject)" :key="i" :label="fieldTypeObject[i]" :value="i" />
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="listenerFieldForm.fieldType === 'string'"
          label="字段值："
          prop="string"
          key="field-string"
          :rules="{ required: true, trigger: ['blur', 'change'] }"
        >
          <el-input v-model="listenerFieldForm.string" clearable />
        </el-form-item>
        <el-form-item
          v-if="listenerFieldForm.fieldType === 'expression'"
          label="表达式："
          prop="expression"
          key="field-expression"
          :rules="{ required: true, trigger: ['blur', 'change'] }"
        >
          <el-input v-model="listenerFieldForm.expression" clearable />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button size="small" @click="listenerFieldFormModelVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="saveListenerFiled">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="UserTaskListeners">
import { createListenerObject, updateElementExtensions } from "../../utils";
import { initListenerForm, initListenerType, eventType, listenerType, fieldType } from "./utilSelf";

const {proxy} = getCurrentInstance()

const props = defineProps({
  id: String,
  type: String
})
const {id,type} = toRefs(props)
const prefix = inject('prefix')
const width = inject('width')
const state = reactive({
  elementListenersList: [],
  listenerEventTypeObject: eventType,
  listenerTypeObject: listenerType,
  listenerFormModelVisible: false,
  listenerForm: {},
  fieldTypeObject: fieldType,
  fieldsListOfListener: [],
  listenerFieldFormModelVisible: false, // 监听器 注入字段表单弹窗 显示状态
  editingListenerIndex: -1, // 监听器所在下标，-1 为新增
  editingListenerFieldIndex: -1, // 字段所在下标，-1 为新增
  listenerFieldForm: {}, // 监听器 注入字段 详情表单
  bpmnElement: undefined,
  bpmnElementListeners: undefined,
  otherExtensionList: []
})
const {elementListenersList,listenerEventTypeObject,listenerTypeObject,listenerFormModelVisible,listenerForm,
  fieldTypeObject,fieldsListOfListener,listenerFieldFormModelVisible,editingListenerIndex,editingListenerFieldIndex,listenerFieldForm} = toRefs(state)

watch(()=>props.id,(val) => {
  val && val.length && proxy.$nextTick(() => resetListenersList());
}, { immediate: true })

function resetListenersList() {
  state.bpmnElement = window.bpmnInstances.bpmnElement;
  state.otherExtensionList = [];
  state.bpmnElementListeners = state.bpmnElement.businessObject?.extensionElements?.values?.filter(ex => ex.$type === `${prefix}:TaskListener`) ?? [];
  state.elementListenersList = state.bpmnElementListeners.map(listener => initListenerType(listener));
}
function openListenerForm(listener, index) {
  if (listener) {
    state.listenerForm = initListenerForm(listener);
    state.editingListenerIndex = index;
  } else {
    state.listenerForm = {};
    state.editingListenerIndex = -1; // 标记为新增
  }
  if (listener && listener.fields) {
    state.fieldsListOfListener = listener.fields.map(field => ({ ...field, fieldType: field.string ? "string" : "expression" }));
  } else {
    state.fieldsListOfListener = [];
    state.listenerForm.fields = []
  }
  // 打开侧边栏并清楚验证状态
  state.listenerFormModelVisible = true;
  proxy.$nextTick(() => {
    if (proxy.$refs["listenerFormRef"]) proxy.$refs["listenerFormRef"].clearValidate();
  });
}
// 移除监听器
function removeListener(listener, index) {
  proxy.$confirm("确认移除该监听器吗？", "提示", {
    confirmButtonText: "确 认",
    cancelButtonText: "取 消"
  })
    .then(() => {
      state.bpmnElementListeners.splice(index, 1);
      state.elementListenersList.splice(index, 1);
      updateElementExtensions(state.bpmnElement, state.otherExtensionList.concat(state.bpmnElementListeners));
    })
    .catch(() => console.info("操作取消"));
}
// 保存监听器
async function saveListenerConfig() {
  let validateStatus = await proxy.$refs["listenerFormRef"].validate();
  if (!validateStatus) return; // 验证不通过直接返回
  const listenerObject = createListenerObject(state.listenerForm, true, prefix);
  if (state.editingListenerIndex === -1) {
    state.bpmnElementListeners.push(listenerObject);
    state.elementListenersList.push(state.listenerForm);
  } else {
    state.bpmnElementListeners.splice(state.editingListenerIndex, 1, listenerObject);
    state.elementListenersList.splice(state.editingListenerIndex, 1, state.listenerForm);
  }
  // 保存其他配置
  state.otherExtensionList = state.bpmnElement.businessObject?.extensionElements?.values?.filter(ex => ex.$type !== `${prefix}:TaskListener`) ?? [];
  updateElementExtensions(state.bpmnElement, state.otherExtensionList.concat(state.bpmnElementListeners));
  // 4. 隐藏侧边栏
  state.listenerFormModelVisible = false;
  state.listenerForm = {};
}
// 打开监听器字段编辑弹窗
function openListenerFieldForm(field, index) {
  state.listenerFieldForm = field ? JSON.parse(JSON.stringify(field)) : {};
  state.editingListenerFieldIndex = field ? index : -1;
  state.listenerFieldFormModelVisible = true;
  proxy.$nextTick(() => {
    if (proxy.$refs["listenerFieldFormRef"]) proxy.$refs["listenerFieldFormRef"].clearValidate();
  });
}
// 保存监听器注入字段
async function saveListenerFiled() {
  let validateStatus = await proxy.$refs["listenerFieldFormRef"].validate();
  if (!validateStatus) return; // 验证不通过直接返回
  if (state.editingListenerFieldIndex === -1) {
    state.fieldsListOfListener.push(state.listenerFieldForm);
    state.listenerForm.fields.push(state.listenerFieldForm);
  } else {
    state.fieldsListOfListener.splice(state.editingListenerFieldIndex, 1, state.listenerFieldForm);
    state.listenerForm.fields.splice(state.editingListenerFieldIndex, 1, state.listenerFieldForm);
  }
  state.listenerFieldFormModelVisible = false;
  proxy.$nextTick(() => (state.listenerFieldForm = {}));
}
// 移除监听器字段
function removeListenerField(field, index) {
  proxy.$confirm("确认移除该字段吗？", "提示", {
    confirmButtonText: "确 认",
    cancelButtonText: "取 消"
  })
    .then(() => {
      state.fieldsListOfListener.splice(index, 1);
      state.listenerForm.fields.splice(index, 1);
    })
    .catch(() => console.info("操作取消"));
}

</script>
